package com.fyakm.daodejing.simple.stockTest.service;


import com.fyakm.daodejing.simple.stockTest.domain.Investors;
import com.fyakm.daodejing.simple.stockTest.domain.StockEntity;

import java.util.Map;

public class ProfitCalculator {

    /**
     * 计算股民的最终盈利
     *
     * @param investor 股民
     * @param stock    股票
     * @return 盈利金额
     */
    public static double calculateProfit(Investors investor, StockEntity stock) {
//        double initialMoney = 100000; // 假设初始现金为 100000
        Integer initialMoney = investor.getInitialMoney();
        double finalMoney = investor.getMoney();

        // 计算持股市值
        for (Map.Entry<String, Integer> entry : investor.getStockMap().entrySet()) {
            String stockCode = entry.getKey();
            int quantity = entry.getValue();
            double price = stock.getHistory().get(stock.getHistory().size() - 1).getClose(); // 取最新价格
            finalMoney += quantity * price;
        }

        return finalMoney - initialMoney;
    }
}